//
//  MarketNoteViewController.m
//  MartketNote
//
//  Created by Nguyen Minh Giang on 1/14/14.
//  Copyright (c) 2014 Nguyen Minh Giang. All rights reserved.
//

#import "MarketNoteViewController.h"

@interface MarketNoteViewController ()

@end

@implementation MarketNoteViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.view.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

//- (NSDictionary *) readFileWithFileName:(NSString *)fileName withTypeFile:(NSString *)typeFile
//{
//    NSString *filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:typeFile];
//    if(fileName)
//        return [[NSDictionary alloc] initWithContentsOfFile:filePath];
//    return NULL;
//}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
//    //read rss
//    NSString *rss = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://kqxs.net.vn/rss-feed/xo-so-dak-lak-xsdlk.rss"] encoding:NSUTF8StringEncoding error:nil];
//    NSLog(@"Content: %@", rss);
    
    //get language
//    languageDictionary = [self readFileWithFileName:@"language" withTypeFile:@"plist"];
//    NSDictionary *item0 = [content valueForKey:@"Item 0"];
//    NSLog(@"Content item 0: %@ -- item 1: %@", [item0 valueForKey:@"Item 0 0"], [item0 valueForKey:@"Item 0 1"]);
//    NSLog(@"String: %@", content);
//    NSLog(@"language = %d", [ReadWriteDataClass getLanguage]);
    
    language = [ReadWriteDataClass getLanguage];
    rowSwipe = -1;
    confirmIndex = -1;
    isDoubleClick = FALSE;
    
    CGSize size = [[UIScreen mainScreen] bounds].size;
    
    tabFocus = TAB_0;
    
    UIImage *imageSave = [UIImage imageNamed:@"save.png"];
    UIImage *imageSaveHover = [UIImage imageNamed:@"save_hv.png"];
    UIImage *imageBack = [UIImage imageNamed:@"back.png"];
    UIImage *imageBackHover = [UIImage imageNamed:@"back_hv.png"];
    
#pragma -mark main view
    //main view
    mainView = [[UIView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:mainView];
    
    //bg Bottom main view
    UIView *bgBottomMain = [[UIView alloc] initWithFrame:CGRectMake(0, size.height - BOTTOM_BAR_HEIGHT, size.width, BOTTOM_BAR_HEIGHT)];
    [mainView addSubview:bgBottomMain];
    
    //line main
    UIView *lineMain = [[UIView alloc] initWithFrame:CGRectMake(0, bgBottomMain.frame.origin.y - LINE_HEIGHT, size.width, LINE_HEIGHT)];
    lineMain.backgroundColor = [UIColor grayColor];
    [mainView addSubview:lineMain];
    
    //btn tab 0 main view
    btnTab0 = [UIButton buttonWithType:UIButtonTypeCustom];
    btnTab0.frame = CGRectMake(0, 0, size.width/2, bgBottomMain.frame.size.height);
    [btnTab0 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    btnTab0.titleLabel.font = [UIFont systemFontOfSize:12];
    [btnTab0 setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
    [btnTab0 setContentEdgeInsets:UIEdgeInsetsMake(22, 0, 0, 0)];
    [btnTab0 addTarget:self action:@selector(tab0Click:) forControlEvents:UIControlEventTouchUpInside];
    [bgBottomMain addSubview:btnTab0];
    
    //icon tab 0
    float scale = 0.8;
    UIImage *imageTab0 = [UIImage imageNamed:@"shop_sl.png"];
    iconTab0 = [[UIImageView alloc] initWithFrame:CGRectMake((btnTab0.frame.size.width - bgBottomMain.frame.size.height*scale)/2, -2, bgBottomMain.frame.size.height*scale, bgBottomMain.frame.size.height*scale)];
    [iconTab0 setImage:imageTab0];
    [btnTab0 addSubview:iconTab0];
    
    //btn tab 1 main view
    btnTab1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btnTab1.frame = CGRectMake(btnTab0.frame.size.width, btnTab0.frame.origin.y, btnTab0.frame.size.width, btnTab0.frame.size.height);
    [btnTab1 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    btnTab1.titleLabel.font = btnTab0.titleLabel.font;
    [btnTab1 setContentHorizontalAlignment:btnTab0.contentHorizontalAlignment];
    [btnTab1 setContentEdgeInsets:btnTab0.contentEdgeInsets];
    [btnTab1 addTarget:self action:@selector(tab1Click:) forControlEvents:UIControlEventTouchUpInside];
    [bgBottomMain addSubview:btnTab1];

    //icon
    UIImage *imageTab1 = [UIImage imageNamed:@"setting_sl.png"];
    iconTab1 = [[UIImageView alloc] initWithFrame:iconTab0.frame];
    [iconTab1 setImage:imageTab1];
    [btnTab1 addSubview:iconTab1];
    
    //btn tab 2 main view
    btnTab2 = [UIButton buttonWithType:UIButtonTypeCustom];
    btnTab2.frame = CGRectMake(btnTab1.frame.origin.x + btnTab1.frame.size.width, btnTab0.frame.origin.y, btnTab0.frame.size.width, btnTab0.frame.size.height);
    [btnTab2 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    btnTab2.titleLabel.font = btnTab0.titleLabel.font;
    [btnTab2 setContentHorizontalAlignment:btnTab0.contentHorizontalAlignment];
    [btnTab2 setContentEdgeInsets:btnTab0.contentEdgeInsets];
    [btnTab2 addTarget:self action:@selector(tab2Click:) forControlEvents:UIControlEventTouchUpInside];
//    [bgBottomMain addSubview:btnTab2];
    
    //icon
    UIImage *imageTab2 = [UIImage imageNamed:@"shop_sl.png"];
    iconTab2 = [[UIImageView alloc] initWithFrame:iconTab0.frame];
    [iconTab2 setImage:imageTab2];
    [btnTab2 addSubview:iconTab2];
    
    [self reloadEventBottomBar];
    
#pragma -mark tab 0 view
    tab0View = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height - BOTTOM_BAR_HEIGHT - LINE_HEIGHT)];
    [mainView addSubview:tab0View];

#pragma -mark tab 0 view main
    tab0MainView = [[UIView alloc] initWithFrame:tab0View.frame];
    [tab0View addSubview:tab0MainView];
    
    //bg top bar 0
    UIView *bgTopBar00 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, TOP_BAR_HEIGHT)];
    bgTopBar00.backgroundColor = [UIColor whiteColor];
    [tab0MainView addSubview:bgTopBar00];
    
    //line tab 0
    UIView *lineTab00 = [[UIView alloc] initWithFrame:CGRectMake(0, TOP_BAR_HEIGHT, size.width, LINE_HEIGHT)];
    lineTab00.backgroundColor = lineMain.backgroundColor;
    [lineTab00.layer setShadowColor:[UIColor clearColor].CGColor];
    [lineTab00.layer setShadowOpacity:0.3f];
    [lineTab00.layer setShadowOffset:CGSizeMake(3.0, 3.0)];
    
    
    //title
    titleTab00 = [[UILabel alloc] initWithFrame:bgTopBar00.frame];
    titleTab00.textAlignment = NSTextAlignmentCenter;
    titleTab00.textColor = [UIColor blackColor];
    titleTab00.font = [UIFont boldSystemFontOfSize:18];
    titleTab00.backgroundColor = [UIColor clearColor];
    [tab0MainView addSubview:titleTab00];
    
    //button right tab 0
    UIButton *btnRightTab00 = [UIButton buttonWithType:UIButtonTypeCustom];
    btnRightTab00.frame = CGRectMake(size.width - bgTopBar00.frame.size.height, 0, bgTopBar00.frame.size.height, bgTopBar00.frame.size.height);
    [btnRightTab00 setBackgroundImage:[UIImage imageNamed:@"add.png"] forState:UIControlStateNormal];
    [btnRightTab00 setSelected:YES];
    [btnRightTab00 setBackgroundImage:[UIImage imageNamed:@"add_hv.png"] forState:UIControlStateSelected | UIControlStateHighlighted];
    [btnRightTab00 addTarget:self action:@selector(rightTab00Click:) forControlEvents:UIControlEventTouchUpInside];
    [tab0MainView addSubview:btnRightTab00];
    
    //button left tab 0
    UIButton *btnLeftTab00 = [UIButton buttonWithType:UIButtonTypeCustom];
    btnLeftTab00.frame = CGRectMake(0, 0, bgTopBar00.frame.size.height, bgTopBar00.frame.size.height);
    [btnLeftTab00 setBackgroundImage:[UIImage imageNamed:@"new.png"] forState:UIControlStateNormal];
    [btnLeftTab00 setSelected:YES];
    [btnLeftTab00 setBackgroundImage:[UIImage imageNamed:@"new_hv.png"] forState:UIControlStateSelected | UIControlStateHighlighted];
    [btnLeftTab00 addTarget:self action:@selector(leftTab00Click:) forControlEvents:UIControlEventTouchUpInside];
    [tab0MainView addSubview:btnLeftTab00];


    
    //table tab 0
    tableTab0 = [[UITableView alloc] initWithFrame:CGRectMake(0, TOP_BAR_HEIGHT + LINE_HEIGHT, size.width, tab0MainView.frame.size.height - TOP_BAR_HEIGHT - LINE_HEIGHT)];
    [tableTab0 setShowsVerticalScrollIndicator:FALSE];
    [tableTab0 setSeparatorColor:[UIColor clearColor]];
    tableTab0.delegate = self;
    tableTab0.dataSource = self;
    [tab0MainView addSubview:tableTab0];
    
    //gesture left for table
    UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeLeft:)];
    [recognizer setDirection:UISwipeGestureRecognizerDirectionLeft];
    recognizer.delegate = self;
    [tableTab0 addGestureRecognizer:recognizer];
    
    //gesture right for table
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeRight:)];
    [recognizer setDirection:UISwipeGestureRecognizerDirectionRight];
    recognizer.delegate = self;
    [tableTab0 addGestureRecognizer:recognizer];

    [tab0MainView addSubview:lineTab00];
    
#pragma -mark tab 0 view detail
    tab0DetailView = [[UIView alloc] initWithFrame:CGRectMake(tab0MainView.frame.size.width, tab0MainView.frame.origin.y, tab0MainView.frame.size.width, tab0MainView.frame.size.height)];
    [tab0View addSubview:tab0DetailView];
    
    //all detail view
    allDetailView = [[UIView alloc] initWithFrame:CGRectMake(0, TOP_BAR_HEIGHT + LINE_HEIGHT, size.width, tab0DetailView.frame.size.height - TOP_BAR_HEIGHT - LINE_HEIGHT)];
    [tab0DetailView addSubview:allDetailView];
    
    //bg top bar 0
    UIView *bgTopBar01 = [[UIView alloc] initWithFrame:bgTopBar00.frame];
    bgTopBar01.backgroundColor = bgTopBar00.backgroundColor;
    [tab0DetailView addSubview:bgTopBar01];
    
    //line tab 0
    UIView *lineTab01 = [[UIView alloc] initWithFrame:lineTab00.frame];
    lineTab01.backgroundColor = lineMain.backgroundColor;
    [lineTab01.layer setShadowColor:lineTab00.layer.shadowColor];
    [lineTab01.layer setShadowOpacity:lineTab00.layer.shadowOpacity];
    [lineTab01.layer setShadowOffset:lineTab00.layer.shadowOffset];
    [tab0DetailView addSubview:lineTab01];
    
    //title
    titleTab01 = [[UILabel alloc] initWithFrame:bgTopBar00.frame];
    titleTab01.textAlignment = titleTab00.textAlignment;
    titleTab01.textColor = titleTab00.textColor;
    titleTab01.font = titleTab00.font;
    titleTab01.backgroundColor = titleTab00.backgroundColor;
    [tab0DetailView addSubview:titleTab01];
    
    //button right tab 0
    UIButton *btnRightTab01 = [UIButton buttonWithType:UIButtonTypeCustom];
    btnRightTab01.frame = btnRightTab00.frame;
    [btnRightTab01 setBackgroundImage:imageSave forState:UIControlStateNormal];
    [btnRightTab01 setSelected:YES];
    [btnRightTab01 setBackgroundImage:imageSaveHover forState:UIControlStateSelected | UIControlStateHighlighted];
    [btnRightTab01 addTarget:self action:@selector(rightTab01Click:) forControlEvents:UIControlEventTouchUpInside];
    [tab0DetailView addSubview:btnRightTab01];
    
    //button left tab 0
    UIButton *btnLeftTab01 = [UIButton buttonWithType:UIButtonTypeCustom];
    btnLeftTab01.frame = btnLeftTab00.frame;
    [btnLeftTab01 setBackgroundImage:imageBack forState:UIControlStateNormal];
    [btnLeftTab01 setSelected:YES];
    [btnLeftTab01 setBackgroundImage:imageBackHover forState:UIControlStateSelected | UIControlStateHighlighted];
    [btnLeftTab01 addTarget:self action:@selector(leftTab01Click:) forControlEvents:UIControlEventTouchUpInside];
    [tab0DetailView addSubview:btnLeftTab01];

#pragma -mark tab 0 view all detail
    
    float jump = 16;
    
    //bg text field 0 detail
    UIView *bgTextFile0Detail = [[UIView alloc] initWithFrame:CGRectMake(15, 20, size.width - 30, 32)];
    bgTextFile0Detail.backgroundColor = [UIColor whiteColor];
    [bgTextFile0Detail.layer setBorderColor:[UIColor grayColor].CGColor];
    [bgTextFile0Detail.layer setBorderWidth:1.0f];
    [bgTextFile0Detail.layer setCornerRadius:4.0];
    [bgTextFile0Detail.layer setShadowColor:lineTab00.layer.shadowColor];
    [bgTextFile0Detail.layer setShadowOpacity:0.2f];
    [bgTextFile0Detail.layer setShadowOffset:CGSizeMake(3.0, 3.0)];
    [bgTextFile0Detail.layer setShadowRadius:bgTextFile0Detail.layer.cornerRadius];
    [allDetailView addSubview:bgTextFile0Detail];
    
    //text field 0 detail
    textField0Detail = [[UITextField alloc] initWithFrame:CGRectMake(bgTextFile0Detail.frame.origin.x + 10, bgTextFile0Detail.frame.origin.y, bgTextFile0Detail.frame.size.width - 20, bgTextFile0Detail.frame.size.height)];
    [textField0Detail setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
    [textField0Detail setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
    textField0Detail.font = [UIFont systemFontOfSize:16];
    textField0Detail.textColor = [UIColor blackColor];
    textField0Detail.delegate = self;
    [allDetailView addSubview:textField0Detail];
    
    
    //bg text file 1 detail
    UIView *bgTextFile1Detail = [[UIView alloc] initWithFrame:CGRectMake(15, bgTextFile0Detail.frame.origin.y + bgTextFile0Detail.frame.size.height + jump, size.width/2 - 20, bgTextFile0Detail.frame.size.height)];
    bgTextFile1Detail.backgroundColor = bgTextFile0Detail.backgroundColor;
    [bgTextFile1Detail.layer setBorderColor:bgTextFile0Detail.layer.borderColor];
    [bgTextFile1Detail.layer setBorderWidth:bgTextFile0Detail.layer.borderWidth];
    [bgTextFile1Detail.layer setCornerRadius:bgTextFile0Detail.layer.cornerRadius];
    [bgTextFile1Detail.layer setShadowColor:bgTextFile0Detail.layer.shadowColor];
    [bgTextFile1Detail.layer setShadowOpacity:bgTextFile0Detail.layer.shadowOpacity];
    [bgTextFile1Detail.layer setShadowOffset:bgTextFile0Detail.layer.shadowOffset];
    [bgTextFile1Detail.layer setShadowRadius:bgTextFile1Detail.layer.cornerRadius];
    [allDetailView addSubview:bgTextFile1Detail];
    
    //text field 1 detail
    textField1Detail = [[UITextField alloc] initWithFrame:CGRectMake(bgTextFile1Detail.frame.origin.x + 10, bgTextFile1Detail.frame.origin.y, bgTextFile1Detail.frame.size.width - 20, bgTextFile1Detail.frame.size.height)];
    [textField1Detail setContentHorizontalAlignment:textField1Detail.contentHorizontalAlignment];
    [textField1Detail setContentVerticalAlignment:textField0Detail.contentVerticalAlignment];
    textField1Detail.font = textField0Detail.font;
    textField1Detail.textColor = textField1Detail.textColor;
    [textField1Detail setKeyboardType:UIKeyboardTypeDecimalPad];
    textField1Detail.delegate = self;
    [allDetailView addSubview:textField1Detail];
    
    //bg text file 2 detail
    UIView *bgTextFile2Detail = [[UIView alloc] initWithFrame:CGRectMake(size.width - 15 - bgTextFile1Detail.frame.size.width, bgTextFile1Detail.frame.origin.y, bgTextFile1Detail.frame.size.width, bgTextFile1Detail.frame.size.height)];
    bgTextFile2Detail.backgroundColor = bgTextFile0Detail.backgroundColor;
    [bgTextFile2Detail.layer setBorderColor:bgTextFile0Detail.layer.borderColor];
    [bgTextFile2Detail.layer setBorderWidth:bgTextFile0Detail.layer.borderWidth];
    [bgTextFile2Detail.layer setCornerRadius:bgTextFile0Detail.layer.cornerRadius];
    [bgTextFile2Detail.layer setShadowColor:bgTextFile0Detail.layer.shadowColor];
    [bgTextFile2Detail.layer setShadowOpacity:bgTextFile0Detail.layer.shadowOpacity];
    [bgTextFile2Detail.layer setShadowOffset:bgTextFile0Detail.layer.shadowOffset];
    [bgTextFile2Detail.layer setShadowRadius:bgTextFile1Detail.layer.cornerRadius];
    [allDetailView addSubview:bgTextFile2Detail];
    
    //text field 2 detail
    textField2Detail = [[UITextField alloc] initWithFrame:CGRectMake(bgTextFile2Detail.frame.origin.x + 10, bgTextFile2Detail.frame.origin.y, bgTextFile2Detail.frame.size.width - 20, bgTextFile2Detail.frame.size.height)];
    [textField2Detail setContentHorizontalAlignment:textField1Detail.contentHorizontalAlignment];
    [textField2Detail setContentVerticalAlignment:textField1Detail.contentVerticalAlignment];
    textField2Detail.font = textField1Detail.font;
    textField2Detail.textColor = textField1Detail.textColor;
    textField2Detail.delegate = self;
    [allDetailView addSubview:textField2Detail];
    
    //bg text file 3 detail
    UIView *bgTextFile3Detail = [[UIView alloc] initWithFrame:CGRectMake(bgTextFile0Detail.frame.origin.x, bgTextFile1Detail.frame.origin.y + bgTextFile1Detail.frame.size.height + jump, bgTextFile0Detail.frame.size.width, bgTextFile0Detail.frame.size.height * 4)];
    bgTextFile3Detail.backgroundColor = bgTextFile0Detail.backgroundColor;
    [bgTextFile3Detail.layer setBorderColor:bgTextFile0Detail.layer.borderColor];
    [bgTextFile3Detail.layer setBorderWidth:bgTextFile0Detail.layer.borderWidth];
    [bgTextFile3Detail.layer setCornerRadius:bgTextFile0Detail.layer.cornerRadius];
    [bgTextFile3Detail.layer setShadowColor:bgTextFile0Detail.layer.shadowColor];
    [bgTextFile3Detail.layer setShadowOpacity:bgTextFile0Detail.layer.shadowOpacity];
    [bgTextFile3Detail.layer setShadowOffset:bgTextFile0Detail.layer.shadowOffset];
    [bgTextFile3Detail.layer setShadowRadius:bgTextFile1Detail.layer.cornerRadius];
    [allDetailView addSubview:bgTextFile3Detail];
    
    //text view 3 detail
    textView3Detail = [[UITextView alloc] initWithFrame:CGRectMake(bgTextFile3Detail.frame.origin.x + 10, bgTextFile3Detail.frame.origin.y, bgTextFile3Detail.frame.size.width - 20, bgTextFile3Detail.frame.size.height)];
    textView3Detail.textColor = [UIColor lightGrayColor];
    textView3Detail.font = textField1Detail.font;
    textView3Detail.backgroundColor = [UIColor clearColor];
    textView3Detail.alpha = 0.7;
    textView3Detail.delegate = self;
    [allDetailView addSubview:textView3Detail];
    
    //button bottom view detail
    UIButton *btnBottomTab01 = [UIButton buttonWithType:UIButtonTypeCustom];
    btnBottomTab01.frame = CGRectMake((allDetailView.frame.size.width - btnLeftTab01.frame.size.width)/2, bgTextFile3Detail.frame.origin.y + bgTextFile3Detail.frame.size.height + 30, btnLeftTab01.frame.size.width, btnLeftTab01.frame.size.height);
    [btnBottomTab01 setBackgroundImage:[UIImage imageNamed:@"delete.png"] forState:UIControlStateNormal];
    [btnBottomTab01 setSelected:YES];
    [btnBottomTab01 setBackgroundImage:[UIImage imageNamed:@"delete_hv.png"] forState:UIControlStateSelected | UIControlStateHighlighted];
    [btnBottomTab01 addTarget:self action:@selector(bottomTab01Click:) forControlEvents:UIControlEventTouchUpInside];
    [allDetailView addSubview:btnBottomTab01];
    
    
#pragma -mark tab 1 view
    
    tab1View = [[UIView alloc] initWithFrame:tab0View.frame];
    [mainView addSubview:tab1View];
    
    //bg top bar 1
    UIView *bgTopBar1 = [[UIView alloc] initWithFrame:bgTopBar00.frame];
    [tab1View addSubview:bgTopBar1];
    
    //line tab 1
    UIView *lineTab1 = [[UIView alloc] initWithFrame:lineTab00.frame];
    lineTab1.backgroundColor = lineTab00.backgroundColor;
    [lineTab1.layer setShadowColor:lineTab00.layer.shadowColor];
    [lineTab1.layer setShadowOpacity:lineTab00.layer.shadowOpacity];
    [lineTab1.layer setShadowOffset:lineTab00.layer.shadowOffset];
    [tab1View addSubview:lineTab1];
    
    //title
    titleTab1 = [[UILabel alloc] initWithFrame:bgTopBar00.frame];
    titleTab1.textAlignment = titleTab00.textAlignment;
    titleTab1.textColor = titleTab00.textColor;
    titleTab1.font = titleTab00.font;
    titleTab1.backgroundColor = titleTab00.backgroundColor;
    [tab1View addSubview:titleTab1];
    
    //lb language
    lbLanguage = [[UILabel alloc] initWithFrame:CGRectMake(10, lineTab1.frame.origin.y + lineTab1.frame.size.height + 20, 200, 30)];
    lbLanguage.textColor = [UIColor blackColor];
    lbLanguage.font = [UIFont systemFontOfSize:16];
    lbLanguage.backgroundColor = [UIColor clearColor];
    [tab1View addSubview:lbLanguage];
    
    //btn vietnamese
    float score = 0.8;
    UIImage *imageVi = [UIImage imageNamed:@"vi.png"];
    UIButton *btnVi = [UIButton buttonWithType:UIButtonTypeCustom];
    btnVi.frame = CGRectMake(size.width/2 - imageVi.size.width * score  - 12.0, lbLanguage.frame.origin.y + lbLanguage.frame.size.height + 20, imageVi.size.width * score, imageVi.size.height * score);
    [btnVi setImage:imageVi forState:UIControlStateNormal];
    [btnVi addTarget:self action:@selector(languageClick:) forControlEvents:UIControlEventTouchUpInside];
    btnVi.tag = LANGUAGE_VI;
    [tab1View addSubview:btnVi];
    
    UIImage *checkImage =[UIImage imageNamed:@"checked.png"];
    //checked Vi
    checkedVi = [[UIImageView alloc] initWithFrame:CGRectMake(btnVi.frame.size.width - 30, btnVi.frame.size.height - 30, 50, 50)];
    [checkedVi setImage:checkImage];
    [btnVi addSubview:checkedVi];
    
    
    //btn english
    UIButton *btnEn = [UIButton buttonWithType:UIButtonTypeCustom];
    btnEn.frame = CGRectMake(size.width/2 + 12.0, btnVi.frame.origin.y, btnVi.frame.size.width, btnVi.frame.size.height);
    [btnEn setImage:[UIImage imageNamed:@"en.png"] forState:UIControlStateNormal];
    [btnEn addTarget:self action:@selector(languageClick:) forControlEvents:UIControlEventTouchUpInside];
    btnEn.tag = LANGUAGE_EN;
    [tab1View addSubview:btnEn];
    
    //checked En
    checkedEn = [[UIImageView alloc] initWithFrame:checkedVi.frame];
    [checkedEn setImage:checkImage];
    [btnEn addSubview:checkedEn];
    
#pragma -mark tab 2 view
    
    tab2View = [[UIView alloc] initWithFrame:tab0View.frame];
    [mainView addSubview:tab2View];
    
    //bg top bar 2
    UIView *bgTopBar2 = [[UIView alloc] initWithFrame:bgTopBar00.frame];
    [tab2View addSubview:bgTopBar2];
    
    //line tab 2
    UIView *lineTab2 = [[UIView alloc] initWithFrame:lineTab00.frame];
    lineTab2.backgroundColor = lineTab00.backgroundColor;
    [lineTab2.layer setShadowColor:lineTab00.layer.shadowColor];
    [lineTab2.layer setShadowOpacity:lineTab00.layer.shadowOpacity];
    [lineTab2.layer setShadowOffset:lineTab00.layer.shadowOffset];
    [tab2View addSubview:lineTab2];
    
    //title
    titleTab2 = [[UILabel alloc] initWithFrame:bgTopBar00.frame];
    titleTab2.textAlignment = titleTab00.textAlignment;
    titleTab2.textColor = titleTab00.textColor;
    titleTab2.font = titleTab00.font;
    titleTab2.backgroundColor = titleTab00.backgroundColor;
    [tab2View addSubview:titleTab2];
    
#pragma -mark global of tab view
    [self reloadAllTabView];
    
    
#pragma -mark add view
    addView = [[UIView alloc] initWithFrame:CGRectMake(0, size.height, size.width, size.height)];
    addView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:addView];
    
    //all add view
    allAddView = [[UIView alloc] initWithFrame:allDetailView.frame];
    [addView addSubview:allAddView];
    
    //bg top bar add view
    UIView *bgTopBarAdd= [[UIView alloc] initWithFrame:bgTopBar00.frame];
    bgTopBarAdd.backgroundColor = bgTopBar00.backgroundColor;
    [addView addSubview:bgTopBarAdd];
    
    //line add view
    UIView *lineTabAdd = [[UIView alloc] initWithFrame:lineTab00.frame];
    lineTabAdd.backgroundColor = lineTab00.backgroundColor;
    [lineTabAdd.layer setShadowColor:lineTab00.layer.shadowColor];
    [lineTabAdd.layer setShadowOpacity:lineTab00.layer.shadowOpacity];
    [lineTabAdd.layer setShadowOffset:lineTab00.layer.shadowOffset];
    [addView addSubview:lineTabAdd];
    
    //title add view
    titleAdd = [[UILabel alloc] initWithFrame:titleTab00.frame];
    titleAdd.textAlignment = titleTab00.textAlignment;
    titleAdd.textColor = titleTab00.textColor;
    titleAdd.font = titleTab00.font;
    titleAdd.backgroundColor = titleTab00.backgroundColor;
    [addView addSubview:titleAdd];
    
    //button right add view
    UIButton *btnRightAdd = [UIButton buttonWithType:UIButtonTypeCustom];
    btnRightAdd.frame = btnRightTab00.frame;
    [btnRightAdd setBackgroundImage:imageSave forState:UIControlStateNormal];
    [btnRightAdd setSelected:YES];
    [btnRightAdd setBackgroundImage:imageSaveHover forState:UIControlStateSelected | UIControlStateHighlighted];
    [btnRightAdd addTarget:self action:@selector(rightAddClick:) forControlEvents:UIControlEventTouchUpInside];
    [addView addSubview:btnRightAdd];
    
    //button left add view
    UIButton *btnLeftAdd = [UIButton buttonWithType:UIButtonTypeCustom];
    btnLeftAdd.frame = btnLeftTab00.frame;
    [btnLeftAdd setBackgroundImage:imageBack forState:UIControlStateNormal];
    [btnLeftAdd setSelected:YES];
    [btnLeftAdd setBackgroundImage:imageBackHover forState:UIControlStateSelected | UIControlStateHighlighted];
    [btnLeftAdd addTarget:self action:@selector(leftAddClick:) forControlEvents:UIControlEventTouchUpInside];
    [addView addSubview:btnLeftAdd];
    
#pragma -mark view all add
    
    //bg text field 0 add
    UIView *bgTextFile0Add = [[UIView alloc] initWithFrame:bgTextFile0Detail.frame];
    bgTextFile0Add.backgroundColor = bgTextFile0Detail.backgroundColor;
    [bgTextFile0Add.layer setBorderColor:bgTextFile0Detail.layer.borderColor];
    [bgTextFile0Add.layer setBorderWidth:bgTextFile0Detail.layer.borderWidth];
    [bgTextFile0Add.layer setCornerRadius:bgTextFile0Detail.layer.cornerRadius];
    [bgTextFile0Add.layer setShadowColor:bgTextFile0Detail.layer.shadowColor];
    [bgTextFile0Add.layer setShadowOpacity:bgTextFile0Detail.layer.shadowOpacity];
    [bgTextFile0Add.layer setShadowOffset:bgTextFile0Detail.layer.shadowOffset];
    [bgTextFile0Add.layer setShadowRadius:bgTextFile0Detail.layer.shadowRadius];
    [allAddView addSubview:bgTextFile0Add];
    
    //text field 0 add
    textField0Add = [[UITextField alloc] initWithFrame:textField0Detail.frame];
    [textField0Add setContentHorizontalAlignment:textField0Detail.contentHorizontalAlignment];
    [textField0Add setContentVerticalAlignment:textField0Detail.contentVerticalAlignment];
    textField0Add.font = textField0Detail.font;
    textField0Add.textColor = textField0Detail.textColor;
    textField0Add.backgroundColor = textField0Detail.backgroundColor;
    textField0Add.delegate = self;
    [allAddView addSubview:textField0Add];
    
    
    //bg text file 1 add
    UIView *bgTextFile1Add = [[UIView alloc] initWithFrame:bgTextFile1Detail.frame];
    bgTextFile1Add.backgroundColor = bgTextFile1Detail.backgroundColor;
    [bgTextFile1Add.layer setBorderColor:bgTextFile1Detail.layer.borderColor];
    [bgTextFile1Add.layer setBorderWidth:bgTextFile1Detail.layer.borderWidth];
    [bgTextFile1Add.layer setCornerRadius:bgTextFile1Detail.layer.cornerRadius];
    [bgTextFile1Add.layer setShadowColor:bgTextFile1Detail.layer.shadowColor];
    [bgTextFile1Add.layer setShadowOpacity:bgTextFile1Detail.layer.shadowOpacity];
    [bgTextFile1Add.layer setShadowOffset:bgTextFile1Detail.layer.shadowOffset];
    [bgTextFile1Add.layer setShadowRadius:bgTextFile1Detail.layer.cornerRadius];
    [allAddView addSubview:bgTextFile1Add];
    
    //text field 1 add
    textField1Add = [[UITextField alloc] initWithFrame:textField1Detail.frame];
    [textField1Add setContentHorizontalAlignment:textField1Detail.contentHorizontalAlignment];
    [textField1Add setContentVerticalAlignment:textField1Detail.contentVerticalAlignment];
    textField1Add.font = textField1Detail.font;
    textField1Add.textColor = textField1Detail.textColor;
    [textField1Add setKeyboardType:textField1Detail.keyboardType];
    textField1Add.delegate = self;
    [allAddView addSubview:textField1Add];
    
    //bg text file 2 add
    UIView *bgTextFile2Add = [[UIView alloc] initWithFrame:bgTextFile2Detail.frame];
    bgTextFile2Add.backgroundColor = bgTextFile2Detail.backgroundColor;
    [bgTextFile2Add.layer setBorderColor:bgTextFile2Detail.layer.borderColor];
    [bgTextFile2Add.layer setBorderWidth:bgTextFile2Detail.layer.borderWidth];
    [bgTextFile2Add.layer setCornerRadius:bgTextFile2Detail.layer.cornerRadius];
    [bgTextFile2Add.layer setShadowColor:bgTextFile2Detail.layer.shadowColor];
    [bgTextFile2Add.layer setShadowOpacity:bgTextFile2Detail.layer.shadowOpacity];
    [bgTextFile2Add.layer setShadowOffset:bgTextFile2Detail.layer.shadowOffset];
    [bgTextFile2Add.layer setShadowRadius:bgTextFile2Detail.layer.cornerRadius];
    [allAddView addSubview:bgTextFile2Add];
    
    //text field 2 add
    textField2Add = [[UITextField alloc] initWithFrame:textField2Detail.frame];
    [textField2Add setContentHorizontalAlignment:textField2Detail.contentHorizontalAlignment];
    [textField2Add setContentVerticalAlignment:textField2Detail.contentVerticalAlignment];
    textField2Add.font = textField2Detail.font;
    textField2Add.textColor = textField2Detail.textColor;
    textField2Add.delegate = self;
    [allAddView addSubview:textField2Add];
    
    //bg text file 3 add
    UIView *bgTextFile3Add = [[UIView alloc] initWithFrame:bgTextFile3Detail.frame];
    bgTextFile3Add.backgroundColor = bgTextFile3Detail.backgroundColor;
    [bgTextFile3Add.layer setBorderColor:bgTextFile3Detail.layer.borderColor];
    [bgTextFile3Add.layer setBorderWidth:bgTextFile0Detail.layer.borderWidth];
    [bgTextFile3Add.layer setCornerRadius:bgTextFile3Detail.layer.cornerRadius];
    [bgTextFile3Add.layer setShadowColor:bgTextFile3Detail.layer.shadowColor];
    [bgTextFile3Add.layer setShadowOpacity:bgTextFile3Detail.layer.shadowOpacity];
    [bgTextFile3Add.layer setShadowOffset:bgTextFile3Detail.layer.shadowOffset];
    [bgTextFile3Add.layer setShadowRadius:bgTextFile3Detail.layer.cornerRadius];
    [allAddView addSubview:bgTextFile3Add];
    
    //text view 3 add
    textView3Add = [[UITextView alloc] initWithFrame:textView3Detail.frame];
    textView3Add.text = [LanguageClass getStringWithLanguge:language withKey:KEY_DETAIL];
    textView3Add.textColor = [UIColor lightGrayColor];
    textView3Add.alpha = 0.7;
    textView3Add.font = textView3Detail.font;
    textView3Add.backgroundColor = textView3Detail.backgroundColor;
    textView3Add.delegate = self;
    [allAddView addSubview:textView3Add];
    
    
#pragma -mark load data
    [self reloadTableData];
    
    [self reloadWhenChangeLanguage];
    
}

#pragma -mark  end viewDidLoad


-(void)leftTab00Click:(id)sender
{
    if([productList count] > 0)
    {
        PopupView *popupView = [[PopupView alloc] initWithFrame:self.view.frame title:[LanguageClass getStringWithLanguge:language withKey:KEY_CREATE_NEW_LIST] message:[LanguageClass getStringWithLanguge:language withKey:KEY_CREATE_NEW_LIST_MESSAGE] titleButtonArray:[NSArray arrayWithObjects:[LanguageClass getStringWithLanguge:language withKey:KEY_CREATE_NEW], [LanguageClass getStringWithLanguge:language withKey:KEY_CLOSE_BUTTON], nil]];
        popupView.delegate = self;
        [self.view addSubview:popupView];
    }
    else
    {
        PopupView *popupView = [[PopupView alloc] initWithFrame:self.view.frame title:[LanguageClass getStringWithLanguge:language withKey:KEY_NOTICE] message:[LanguageClass getStringWithLanguge:language withKey:KEY_NEW_LIST_MESSAGE]];
        [self.view addSubview:popupView];
    }
}

-(void)rightTab00Click:(id)sender
{
    
    [self addViewShow];
}

-(void)leftTab01Click:(id)sender
{
    [self resignFirstResponderTabDetail];
    [self detailViewHide];
}

-(void)rightTab01Click:(id)sender
{
    [self resignFirstResponderTabDetail];
    if([self checkText0:textField0Detail.text text1:textField1Detail.text text2:textField2Detail.text])
    {
        NSString *detail = textView3Detail.text;
        if([detail isEqualToString:[LanguageClass getStringWithLanguge:language withKey:KEY_DETAIL]])
            detail = @"";
        [self leftTab01Click:sender];
        [ReadWriteDataClass updateProductWithID:productSelected newName:textField0Detail.text newQuantity:[textField1Detail.text floatValue] newUnit:textField2Detail.text newDetail:detail newStatus:NO];
        [self reloadTableData];
    }
}

-(void)bottomTab01Click:(id)sender
{
    [ReadWriteDataClass deleteManagedObject:[productList objectAtIndex:indexPathSelected.row]];
    [productList removeObjectAtIndex:indexPathSelected.row];
    [tableTab0 reloadData];
    
    [self leftTab01Click:sender];

}

-(void)leftAddClick:(id)sender
{
    [self resignFirstResponderTabAdd];
    [self addViewHide];
    
}

-(void)rightAddClick:(id)sender
{
    [self resignFirstResponderTabAdd];
    if([self checkText0:textField0Add.text text1:textField1Add.text text2:textField2Add.text])
    {
        [self leftAddClick:sender];
        NSString *detail = textView3Add.text;
        if([detail isEqualToString:[LanguageClass getStringWithLanguge:language withKey:KEY_DETAIL]])
            detail = @"";
        [ReadWriteDataClass insertNewProductWithID:[ReadWriteDataClass getProductID] name:textField0Add.text quantity:[textField1Add.text floatValue] unit:textField2Add.text detail:detail status:NO];
        [self reloadTableData];
    }
}

-(void)tab0Click:(id)sender
{
    if(tabFocus != TAB_0)
    {
        tabFocus = TAB_0;
        [self reloadEventBottomBar];
        [self reloadAllTabView];
    }
}

-(void)tab1Click:(id)sender
{
    if(tabFocus != TAB_1)
    {
        tabFocus = TAB_1;
        [self reloadEventBottomBar];
        [self reloadAllTabView];
    }
}

-(void)tab2Click:(id)sender
{
    if(tabFocus != TAB_2)
    {
        tabFocus = TAB_2;
        [self reloadEventBottomBar];
        [self reloadAllTabView];
    }
}

-(void)handleSwipeLeft:(UISwipeGestureRecognizer *)gestureRecognizer
{
    if(rowSwipe == -1)
    {
        CGPoint location = [gestureRecognizer locationInView:tableTab0];
        
        NSIndexPath *indexPath = [tableTab0 indexPathForRowAtPoint:location];
        
        if(indexPath)
        {
            UITableViewCell *cell = [tableTab0 cellForRowAtIndexPath:indexPath];
            
            [UIView beginAnimations:Nil context:Nil];
            [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
            [UIView setAnimationDuration:0.3];
            for(UIView *v in [cell.contentView subviews])
                if([v tag] >= TAG_BUTTON_BEGIN)
                    v.frame = CGRectMake(v.frame.origin.x - TOP_BAR_HEIGHT - 30, v.frame.origin.y, v.frame.size.width, v.frame.size.height);
            [UIView commitAnimations];
            
            rowSwipe = indexPath.row;
        }
    }
}

-(void)handleSwipeRight:(UISwipeGestureRecognizer *)gestureRecognizer
{
    if(rowSwipe != -1)
    {
        CGPoint location = [gestureRecognizer locationInView:tableTab0];
        
        NSIndexPath *indexPath = [tableTab0 indexPathForRowAtPoint:location];
        
        if(indexPath && indexPath.row == rowSwipe)
        {
//            UITableViewCell *cell = [tableTab0 cellForRowAtIndexPath:indexPath];
//            
//            [UIView beginAnimations:Nil context:Nil];
//            [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
//            [UIView setAnimationDuration:0.3];
//            for(UIView *v in [cell.contentView subviews])
//                if([v tag] >= TAG_BUTTON_BEGIN)
//                    v.frame = CGRectMake(v.frame.origin.x + TOP_BAR_HEIGHT + 30, v.frame.origin.y, v.frame.size.width, v.frame.size.height);
//            [UIView commitAnimations];
//            
//            
//            
//            rowSwipe = -1;
            
            [self refreshRow];
        }
    }
}

-(void)btnRightTableViewCellClick:(id)sender
{
    int index = [sender tag] - TAG_BUTTON_BEGIN;
    [ReadWriteDataClass deleteManagedObject:[productList objectAtIndex:index]];
    [productList removeObjectAtIndex:index];
    [tableTab0 reloadData];
    
}

-(void)btnCheckClick:(id)sender
{
    NSLog(@"Tap click");
    [self performSelector:@selector(btnCheckSingleClick:) withObject:sender afterDelay:0.5];
}

-(void)btnCheckSingleClick:(id)sender
{
    if(!isDoubleClick)
        NSLog(@"Tap one run ");
    
    isDoubleClick = FALSE;
//    if(rowSwipe == -1)
//    {
//        confirmIndex = [sender tag] - TAG_BUTTON_BEGIN;
//        NSManagedObject *product = [productList objectAtIndex:confirmIndex];
//        if(![[product valueForKey:ATT_STATUS] boolValue])
//        {
//            PopupView *popupView = [[PopupView alloc] initWithFrame:self.view.frame title:[LanguageClass getStringWithLanguge:language withKey:KEY_CONFIRM] message:[NSString stringWithFormat:@"%@ %@?", [LanguageClass getStringWithLanguge:language withKey:KEY_CONFIRM_MESSAGE], [product valueForKey:ATT_PRODUCT]] titleButtonArray:[NSArray arrayWithObjects:[LanguageClass getStringWithLanguge:language withKey:KEY_BOUGHT], [LanguageClass getStringWithLanguge:language withKey:KEY_CLOSE_BUTTON], nil]];
//            popupView.delegate = self;
//            [self.view addSubview:popupView];
//        }
//        
//    }
//    else
//    {
//        [self refreshRow];
//    }
}

-(void)btnCheckDoubleClick:(id)sender
{
//    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(btnCheckSingleClick:) object:sender];
    NSLog(@"Double Click checked");
    isDoubleClick = TRUE;
//    if(rowSwipe == -1)
//    {
//        NSManagedObject *product = [productList objectAtIndex:[sender tag] - TAG_BUTTON_BEGIN];
//        if(![[product valueForKey:ATT_STATUS] boolValue])
//        {
//            [ReadWriteDataClass updateStatusProduct:product];
//            [tableTab0 reloadData];
//        }
//    }
//    else
//        [self refreshRow];
}

-(void)multiTapbtnCheck:(id)sender withEvent:(UIEvent *)event
{
    NSLog(@"multiTapbtnCheck");
    if(rowSwipe == -1)
    {
        UITouch *touch = [[event allTouches] anyObject];
        if(touch.tapCount == 1)
        {
            NSLog(@"Tap 1");
        }
        else
        {
            if(touch.tapCount == 2)
                NSLog(@"Tap 2");
        }
//        NSManagedObject *product = [productList objectAtIndex:[sender tag] - TAG_BUTTON_BEGIN];
//        if(![[product valueForKey:ATT_STATUS] boolValue])
//        {
//            [ReadWriteDataClass updateStatusProduct:product];
//            [tableTab0 reloadData];
//        }
    }
    else
        [self refreshRow];
}

-(void)languageClick:(id)sender
{
    //set language
    language = [sender tag];
    [ReadWriteDataClass updateLanguageWithNewLanguage:[sender tag]];
    
    [self reloadWhenChangeLanguage];
}

#pragma mark - other functions
-(void)sortProductList
{
    int count = [productList count];
    NSMutableArray *checked = [[NSMutableArray alloc] init];
    NSMutableArray *uncheck = [[NSMutableArray alloc] init];
    for(int i=0; i<count; i++)
    {
        NSManagedObject *product = [productList objectAtIndex:i];
        if([[product valueForKey:ATT_STATUS] boolValue])
            [checked addObject:product];
        else
            [uncheck addObject:product];
    }
    NSMutableArray *new = [[NSMutableArray alloc] init];
    for(int i=0; i<[uncheck count]; i++)
    {
        NSManagedObject *product = [uncheck objectAtIndex:i];
        [new addObject:product];
    }
    for(int i=0; i<[checked count]; i++)
    {
        NSManagedObject *product = [checked objectAtIndex:i];
        [new addObject:product];
    }
    productList = new;
}

-(void)refreshRow
{
    UITableViewCell *cell = [tableTab0 cellForRowAtIndexPath:[NSIndexPath indexPathForRow:rowSwipe inSection:0]];
    
    [UIView beginAnimations:Nil context:Nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:0.3];
    for(UIView *v in [cell.contentView subviews])
        if([v tag] >= TAG_BUTTON_BEGIN)
            v.frame = CGRectMake(v.frame.origin.x + TOP_BAR_HEIGHT + 30, v.frame.origin.y, v.frame.size.width, v.frame.size.height);
    [UIView commitAnimations];
    
    
    
    rowSwipe = -1;
}


-(void)reloadWhenChangeLanguage
{
    
    titleTab00.text = [LanguageClass getStringWithLanguge:language withKey:KEY_MARKET_NOTE];
    titleTab01.text = [LanguageClass getStringWithLanguge:language withKey:KEY_DETAIL];
    titleTab1.text = [LanguageClass getStringWithLanguge:language withKey:KEY_SETTING];
    titleTab2.text = [LanguageClass getStringWithLanguge:language withKey:KEY_ABOUT];
    titleAdd.text = [LanguageClass getStringWithLanguge:language withKey:KEY_ADD_NEW];
    
    [btnTab2 setTitle:[LanguageClass getStringWithLanguge:language withKey:KEY_ABOUT] forState:UIControlStateNormal];
    [btnTab0 setTitle:[LanguageClass getStringWithLanguge:language withKey:KEY_MARKET] forState:UIControlStateNormal];
    [btnTab1 setTitle:[LanguageClass getStringWithLanguge:language withKey:KEY_SETTING] forState:UIControlStateNormal];
    
    
    textField0Detail.placeholder = [LanguageClass getStringWithLanguge:language withKey:KEY_PRODUCT_NAME];
    textField1Detail.placeholder = [LanguageClass getStringWithLanguge:language withKey:KEY_QUANTITY];
    textField2Detail.placeholder = [LanguageClass getStringWithLanguge:language withKey:KEY_UNIT];
    if([textView3Detail.text isEqualToString:[LanguageClass getStringWithLanguge:LANGUAGE_VI withKey:KEY_DETAIL]] || [textView3Detail.text isEqualToString:[LanguageClass getStringWithLanguge:LANGUAGE_EN withKey:KEY_DETAIL]])
    textView3Detail.text = [LanguageClass getStringWithLanguge:language withKey:KEY_DETAIL];
    
    textField0Add.placeholder = textField0Detail.placeholder;
    textField1Add.placeholder = textField1Detail.placeholder;
    textField2Add.placeholder = textField2Detail.placeholder;
    if([textView3Add.text isEqualToString:[LanguageClass getStringWithLanguge:LANGUAGE_VI withKey:KEY_DETAIL]] || [textView3Add.text isEqualToString:[LanguageClass getStringWithLanguge:LANGUAGE_EN withKey:KEY_DETAIL]])
        textView3Add.text = [LanguageClass getStringWithLanguge:language withKey:KEY_DETAIL];
    
    
    lbLanguage.text = [LanguageClass getStringWithLanguge:language withKey:KEY_LANGUAGE];
    
    if(language == LANGUAGE_EN)
    {
        [checkedEn setHidden:FALSE];
        [checkedVi setHidden:TRUE];
    }
    else
    {
        [checkedEn setHidden:TRUE];
        [checkedVi setHidden:FALSE];
    }
    
}

-(void)resignFirstResponderTabAdd
{
    [textField0Add resignFirstResponder];
    [textField1Add resignFirstResponder];
    [textField2Add resignFirstResponder];
    [textView3Add resignFirstResponder];
}

-(void)resignFirstResponderTabDetail
{
    [textField0Detail resignFirstResponder];
    [textField1Detail resignFirstResponder];
    [textField2Detail resignFirstResponder];
    [textView3Detail resignFirstResponder];
}

-(BOOL)checkFloatOfText:(NSString *)text
{
    for(int i=0; i<[text length]; i++)
    {
        NSString *char0 = [text substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [NUMBER_ARRAY rangeOfString:char0];
        if(range.location == NSNotFound)
            return FALSE;
    }
    
    int countDos = 0;
    for(int i=0; i<[text length]; i++)
    {
        NSString *char0 = [text substringWithRange:NSMakeRange(i, 1)];
        if([char0 isEqualToString:@"."])
            countDos ++;
    }
    if(countDos > 1)
        return FALSE;
    return TRUE;
}

-(BOOL)checkText0:(NSString *)text0 text1:(NSString *)text1 text2:(NSString *)text2
{
    NSString *title = [LanguageClass getStringWithLanguge:language withKey:KEY_NOTICE];
    if([text0 isEqualToString:@""])
    {
        PopupView *popupView = [[PopupView alloc] initWithFrame:self.view.frame title:title message:[LanguageClass getStringWithLanguge:language withKey:KEY_NULL_PRODUCT]];
        [self.view addSubview:popupView];
        return FALSE;
    }
    if([text1 isEqualToString:@""])
    {
        PopupView *popupView = [[PopupView alloc] initWithFrame:self.view.frame title:title message:[LanguageClass getStringWithLanguge:language withKey:KEY_NULL_QUANTITY]];
        [self.view addSubview:popupView];
        return FALSE;
    }
    if(![self checkFloatOfText:text1])
    {
        PopupView *popupView = [[PopupView alloc] initWithFrame:self.view.frame title:title message:[LanguageClass getStringWithLanguge:language withKey:KEY_UNFORMAT]];
        [self.view addSubview:popupView];
        return FALSE;
    }
    if([text2 isEqualToString:@""])
    {
        PopupView *popupView = [[PopupView alloc] initWithFrame:self.view.frame title:title message:[LanguageClass getStringWithLanguge:language withKey:KEY_NULL_UNIT]];
        [self.view addSubview:popupView];
        return FALSE;
    }
    return TRUE;
}

-(NSString *)formatQuantityWith:(NSString *)quantity
{
    if([quantity length] <= 1)
        return quantity;
    NSRange range = [quantity rangeOfString:@"."];
    if(range.length <= 0)
        return quantity;
    NSString *endChar = [quantity substringWithRange:NSMakeRange([quantity length] - 1, 1)];
    
    if(![endChar isEqualToString:@"0"] && ![endChar isEqualToString:@"."])
        return quantity;
    quantity = [quantity substringWithRange:NSMakeRange(0, [quantity length] - 1)];
    [self formatQuantityWith:quantity];
    return @"";
}
-(void)addViewShow
{
    textField0Add.text = @"";
    textField1Add.text = @"";
    textField2Add.text = @"";
    textView3Add.text = [LanguageClass getStringWithLanguge:language withKey:KEY_DETAIL];
    textView3Add.textColor = [UIColor lightGrayColor];
    textView3Add.alpha = 0.7;
    
    [UIView beginAnimations:Nil context:Nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:0.4];
    addView.frame = CGRectMake(addView.frame.origin.x, 0, addView.frame.size.width, addView.frame.size.height);
    [UIView commitAnimations];
}

-(void)addViewHide
{
    [UIView beginAnimations:Nil context:Nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:0.4];
    addView.frame = CGRectMake(addView.frame.origin.x, addView.frame.size.height, addView.frame.size.width, addView.frame.size.height);
    [UIView commitAnimations];
}

-(void)detailViewShow
{
    textField0Detail.text = [productSelected valueForKey:ATT_PRODUCT];
    textField1Detail.text = [NSString stringWithFormat:@"%@", [self formatQuantityWith:[NSString stringWithFormat:@"%@", [productSelected valueForKey:ATT_QUANTITY]]]];
    textField2Detail.text = [productSelected valueForKey:ATT_UNIT];
    textView3Detail.text = [productSelected valueForKey:ATT_DETAIL];
    if(![textView3Detail.text isEqualToString:[LanguageClass getStringWithLanguge:language withKey:KEY_DETAIL]])
    {
        textView3Detail.textColor = [UIColor blackColor];
        textView3Detail.alpha = 1.0;
    }
    else
    {
        textView3Detail.textColor = [UIColor lightGrayColor];
        textView3Detail.alpha = 0.7;
    }
    
    [UIView beginAnimations:Nil context:Nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:0.4];
    tab0MainView.frame = CGRectMake(-tab0MainView.frame.size.width, tab0MainView.frame.origin.y, tab0MainView.frame.size.width, tab0MainView.frame.size.height);
    tab0DetailView.frame = CGRectMake(0, tab0DetailView.frame.origin.y, tab0DetailView.frame.size.width, tab0DetailView.frame.size.height);
    [UIView commitAnimations];
}

-(void)detailViewHide
{
    [UIView beginAnimations:Nil context:Nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:0.4];
    tab0MainView.frame = CGRectMake(0, tab0MainView.frame.origin.y, tab0MainView.frame.size.width, tab0MainView.frame.size.height);
    tab0DetailView.frame = CGRectMake(tab0DetailView.frame.size.width, tab0DetailView.frame.origin.y, tab0DetailView.frame.size.width, tab0DetailView.frame.size.height);
    [UIView commitAnimations];
}

-(void)reloadEventBottomBar
{
    UIImage *imageTab0Focus = [UIImage imageNamed:@"shop_sl.png"];
    UIImage *imageTab0UnFocus = [UIImage imageNamed:@"shop_not_sl.png"];
    UIImage *imageTab1Focus = [UIImage imageNamed:@"setting_sl.png"];
    UIImage *imageTab1UnFocus = [UIImage imageNamed:@"setting_not_sl.png"];
    UIImage *imageTab2Focus = [UIImage imageNamed:@"shop_sl.png"];
    UIImage *imageTab2UnFocus = [UIImage imageNamed:@"shop_not_sl.png"];
    
    [iconTab0 setImage:imageTab0UnFocus];
    [iconTab1 setImage:imageTab1UnFocus];
    [iconTab2 setImage:imageTab2UnFocus];
    
    [btnTab0 setTitleColor:[UIColor colorWithRed:0.57 green:0.57 blue:0.57 alpha:1.0] forState:UIControlStateNormal];
    [btnTab1 setTitleColor:[UIColor colorWithRed:0.57 green:0.57 blue:0.57 alpha:1.0] forState:UIControlStateNormal];
    [btnTab2 setTitleColor:[UIColor colorWithRed:0.57 green:0.57 blue:0.57 alpha:1.0] forState:UIControlStateNormal];
    
    switch(tabFocus)
    {
        case TAB_0:
        {
            [iconTab0 setImage:imageTab0Focus];
            [btnTab0 setTitleColor:[UIColor colorWithRed:0.0 green:0.48 blue:1.0 alpha:1.0] forState:UIControlStateNormal];
            //            [marketTable setHidden:FALSE];
            //            [settingView setHidden:TRUE];
            //            [aboutView setHidden:TRUE];
            break;
        }
        case TAB_1:
        {
            [iconTab1 setImage:imageTab1Focus];
            [btnTab1 setTitleColor:[UIColor colorWithRed:0.0 green:0.48 blue:1.0 alpha:1.0] forState:UIControlStateNormal];
            //            [marketTable setHidden:TRUE];
            //            [settingView setHidden:FALSE];
            //            [aboutView setHidden:TRUE];
            break;
        }
        case TAB_2:
        {
            [iconTab2 setImage:imageTab2Focus];
            [btnTab2 setTitleColor:[UIColor colorWithRed:0.0 green:0.48 blue:1.0 alpha:1.0] forState:UIControlStateNormal];
            //            [marketTable setHidden:TRUE];
            //            [settingView setHidden:TRUE];
            //            [aboutView setHidden:FALSE];
            break;
        }
    }
}

-(void)reloadAllTabView
{
    [tab0View setHidden:YES];
    [tab1View setHidden:YES];
    [tab2View setHidden:YES];
    
    switch(tabFocus)
    {
        case TAB_0:
        {
            [tab0View setHidden:FALSE];
            break;
        }
        case TAB_1:
        {
            [tab1View setHidden:FALSE];
            break;
        }
        case TAB_2:
        {
            [tab2View setHidden:FALSE];
            break;
        }
    }
    
}

-(void)reloadTableData
{
    productList = [ReadWriteDataClass getDataProducts];
    [tableTab0 reloadData];
}


#pragma mark - table view delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [productList count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return TABLE_VIEW_CELL_HEIGHT;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIndetifier = @"Cell_Connections";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIndetifier];
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIndetifier];
    cell.backgroundColor = [UIColor clearColor];
    
    [self sortProductList];
    NSManagedObject *product = [productList objectAtIndex:indexPath.row];
    
    int tag = TAB_TABLE_BEGIN + indexPath.row;
    if(rowSwipe != -1)
        rowSwipe = -1;
    
    //name product
    UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(16, 2.0, cell.frame.size.width*2/3 - 20, 32)];
    name.text = [NSString stringWithFormat:@"%@", [product valueForKey:ATT_PRODUCT]];
    name.textColor = [UIColor blackColor];
    name.font = [UIFont systemFontOfSize:22];
    name.backgroundColor = [UIColor clearColor];
    name.tag = tag;
    [cell.contentView addSubview:name];
    
    //quantity product
    UILabel *quantity = [[UILabel alloc] initWithFrame:CGRectMake(name.frame.origin.x, name.frame.origin.y + name.frame.size.height, name.frame.size.width, 30)];
    quantity.text = [NSString stringWithFormat:@"%@ %@", [self formatQuantityWith:[NSString stringWithFormat:@"%@", [product valueForKey:ATT_QUANTITY]]], [product valueForKey:ATT_UNIT]];
    quantity.textColor = [UIColor blackColor];
    quantity.font = [UIFont systemFontOfSize:15];
    quantity.backgroundColor = [UIColor clearColor];
    quantity.tag = tag;
    [cell.contentView addSubview:quantity];
    
    //detail
    UILabel *detail = [[UILabel alloc] initWithFrame:CGRectMake(name.frame.origin.x, quantity.frame.origin.y + quantity.frame.size.height, cell.frame.size.width - 30, quantity.frame.size. height)];
    detail.text = [product valueForKey:ATT_DETAIL];
    detail.textColor = [UIColor grayColor];
    detail.font = [UIFont systemFontOfSize:15];
    detail.backgroundColor = [UIColor clearColor];
    detail.tag = tag;
    [cell.contentView addSubview:detail];
    
    //btn check
    BOOL isDone = [[product valueForKey:ATT_STATUS] boolValue];
    UIImage *imgChecked = [UIImage imageNamed:@"checked.png"];
    UIImage *imgUnCheck = [UIImage imageNamed:@"uncheck.png"];
    UIButton *btnCheck = [UIButton buttonWithType:UIButtonTypeCustom];
    btnCheck.frame = CGRectMake(tableView.frame.size.width - TOP_BAR_HEIGHT - 30, (TABLE_VIEW_CELL_HEIGHT - TOP_BAR_HEIGHT)/2, TOP_BAR_HEIGHT, TOP_BAR_HEIGHT);
    if(isDone)
    {
        [btnCheck setImage:imgChecked forState:UIControlStateNormal];
    }
    else
    {
        [btnCheck setImage:imgUnCheck forState:UIControlStateNormal];
    }
    btnCheck.tag = tag;
    [btnCheck addTarget:self action:@selector(btnCheckClick:) forControlEvents:UIControlEventTouchUpInside];
    [btnCheck addTarget:self action:@selector(btnCheckDoubleClick:) forControlEvents:UIControlEventTouchDownRepeat];
    
    [cell.contentView addSubview:btnCheck];
    
//    //single tap
//    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(btnCheckClick:)];
//    singleTap.numberOfTouchesRequired = 1;
////    singleTap.delegate = self;
//    
//    //double tap
//    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(btnCheckDoubleClick:)];
//    doubleTap.numberOfTouchesRequired = 2;
//    
//    
//    [singleTap requireGestureRecognizerToFail:doubleTap];
////    doubleTap.delegate = self;
//    [btnCheck addGestureRecognizer:singleTap];
//    [btnCheck addGestureRecognizer:doubleTap];
    
    
    
    //btn right in table
    UIButton *btnRightTable = [UIButton buttonWithType:UIButtonTypeCustom];
    btnRightTable.frame = CGRectMake(tableView.frame.size.width + 15, (TABLE_VIEW_CELL_HEIGHT - TOP_BAR_HEIGHT)/2, TOP_BAR_HEIGHT, TOP_BAR_HEIGHT);
    [btnRightTable setBackgroundImage:[UIImage imageNamed:@"delete.png"] forState:UIControlStateNormal];
    [btnRightTable setSelected:YES];
    [btnRightTable setBackgroundImage:[UIImage imageNamed:@"delete_hv.png"] forState:UIControlStateHighlighted|UIControlStateSelected];
    [btnRightTable addTarget:self action:@selector(btnRightTableViewCellClick:) forControlEvents:UIControlEventTouchUpInside];
    btnRightTable.tag = tag;
    [cell.contentView addSubview:btnRightTable];
    
    //line
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(30, TABLE_VIEW_CELL_HEIGHT - LINE_HEIGHT, tableView.frame.size.width - 60, LINE_HEIGHT)];
    line.backgroundColor = [UIColor lightGrayColor];
//    [line.layer setShadowColor:[UIColor grayColor].CGColor];
//    [line.layer setShadowOpacity:0.4f];
//    [line.layer setShadowOffset:CGSizeMake(0.0, 1.0)];
    line.tag = tag;
    [cell.contentView addSubview:line];
    
//    cell.contentView.backgroundColor = [UIColor greenColor];
    
    //
    
    return cell;
}

-(void)deselect
{
    [tableTab0 deselectRowAtIndexPath:[tableTab0 indexPathForSelectedRow] animated:YES];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(rowSwipe != -1)
    {
        [self refreshRow];
        //deselect
        [self performSelector:@selector(deselect) withObject:nil afterDelay:0.0];
    }
    else
    {
        indexPathSelected = indexPath;
        productSelected = [productList objectAtIndex:indexPath.row];
        [self detailViewShow];
        
        //deselect
        [self performSelector:@selector(deselect) withObject:nil afterDelay:0.01];
    }
}


#pragma mark - text field delegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    return [textField resignFirstResponder];
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if(textField == textField0Add || textField == textField0Detail)
    {
        NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
        if([newString length] > 20)
        {
            [textField resignFirstResponder];
            PopupView *popupView = [[PopupView alloc] initWithFrame:self.view.frame title:[LanguageClass getStringWithLanguge:language withKey:KEY_NOTICE] message:[LanguageClass getStringWithLanguge:language withKey:KEY_LIMIT_NAME]];
            [self.view addSubview:popupView];
            return FALSE;
        }
    }
    if(textField == textField2Add || textField == textField2Detail)
    {
        NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
        if([newString length] > 10)
        {
            [textField resignFirstResponder];
            PopupView *popupView = [[PopupView alloc] initWithFrame:self.view.frame title:[LanguageClass getStringWithLanguge:language withKey:KEY_NOTICE] message:[LanguageClass getStringWithLanguge:language withKey:KEY_LIMIT_UNIT]];
            [self.view addSubview:popupView];
            return FALSE;
        }
    }
    return YES;
}


#pragma mark - text view delegate
-(void)textViewDidBeginEditing:(UITextView *)textView
{
    if([textView.text isEqualToString:[LanguageClass getStringWithLanguge:language withKey:KEY_DETAIL]])
    {
        textView.text = @"";
        textView.textColor = [UIColor blackColor];
        textView.alpha = 1.0;
    }
    [UIView beginAnimations:Nil context:Nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:0.4];
    if(textView == textView3Detail)
        allDetailView.frame = CGRectMake(allDetailView.frame.origin.x, allDetailView.frame.origin.y - 40, allDetailView.frame.size.width, allDetailView.frame.size.height);
    else
        allAddView.frame = CGRectMake(allAddView.frame.origin.x, allAddView.frame.origin.y - 40, allAddView.frame.size.width, allAddView.frame.size.height);
    [UIView commitAnimations];
}

-(void)textViewDidEndEditing:(UITextView *)textView
{
    if([textView.text isEqualToString:@""])
    {
        textView.text = [LanguageClass getStringWithLanguge:language withKey:KEY_DETAIL];
        textView.textColor = [UIColor lightGrayColor];
        textView.alpha = 0.7;
    }
    [UIView beginAnimations:Nil context:Nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:0.4];
    if(textView == textView3Detail)
        allDetailView.frame = CGRectMake(allDetailView.frame.origin.x, allDetailView.frame.origin.y + 40, allDetailView.frame.size.width, allDetailView.frame.size.height);
    else
        allAddView.frame = CGRectMake(allAddView.frame.origin.x, allAddView.frame.origin.y + 40, allAddView.frame.size.width, allAddView.frame.size.height);
    [UIView commitAnimations];
}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
//    NSString *newString = [textView.text stringByReplacingCharactersInRange:range withString:text];
//    if([newString isEqualToString:@""])
//    {
//        textView.text = @"Detail";
//        textView.textColor = [UIColor lightGrayColor];
//        textView.alpha = 0.7;
//    }
    return YES;
}

#pragma mark - touch delegate
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self resignFirstResponderTabDetail];
    [self resignFirstResponderTabAdd];
}

#pragma mark - popup view delegate
-(void)popupViewEventButtonDelegate:(id)sender
{
    if(confirmIndex != -1)
    {
        NSManagedObject *product = [productList objectAtIndex:confirmIndex];
        if(![[product valueForKey:ATT_STATUS] boolValue])
        {
            [ReadWriteDataClass updateStatusProduct:product];
            [tableTab0 reloadData];
        }
        confirmIndex = -1;
    }
    else
    {
        [ReadWriteDataClass deleteAllData];
        [self reloadTableData];
    }
}

////gesture delegate
//-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
//{
//    return YES;
//}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
